package com.rc.web.converter;

import com.rc.pojo.po.Activity;
import com.rc.pojo.po.Adoption;
import com.rc.pojo.po.Farm;
import com.rc.pojo.po.SearchDoc;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
 * 搜索文档转换器
 * 使用MapStruct实现不同POJO到SearchDoc的转换
 */
@Mapper(componentModel = "spring") // 声明为Spring组件
public interface SearchConverter {

    /**
     * 将Activity转换为SearchDoc
     * @param activity 活动实体
     * @return 搜索文档
     */
    @Mapping(target = "itemId",source = "id") // 将id映射到itemId
    @Mapping(target = "type",expression = "java(com.rc.common.constant.CollectType.ACTIVITY)") // 设置类型为ACTIVITY
    @Mapping(target = "cover",expression= "java(activity.getImgs().stream().findFirst().orElse(new com.rc.common.model.ImageModel()).getUrl())") // 获取第一张图片作为封面
    SearchDoc toDoc(Activity activity);

    /**
     * 将Farm转换为SearchDoc
     * @param farm 农场实体
     * @return 搜索文档
     */
    @Mapping(target = "itemId",source = "id") // 将id映射到itemId
    @Mapping(target = "title",source = "name") // 将name映射到title
    @Mapping(target = "type",expression = "java(com.rc.common.constant.CollectType.FARM)") // 设置类型为FARM
    @Mapping(target = "cover",expression = "java(farm.getImgs().stream().findFirst().orElse(new com.rc.common.model.ImageModel()).getUrl())") // 获取第一张图片作为封面
    SearchDoc toDoc(Farm farm);

    /**
     * 将Adoption转换为SearchDoc
     * @param adoption 领养实体
     * @return 搜索文档
     */
    @Mapping(target = "itemId",source = "id") // 将id映射到itemId
    @Mapping(target = "type",expression = "java(com.rc.common.constant.CollectType.ADOPTION)") // 设置类型为ADOPTION
    @Mapping(target = "cover",expression = "java(adoption.getImgs().stream().findFirst().orElse(new com.rc.common.model.ImageModel()).getUrl())") // 获取第一张图片作为封面
    SearchDoc toDoc(Adoption adoption);
}
